package com.xiyili.topspecialty;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xiyili.topspecialty.data.FineCourse;
import com.xiyili.topspecialty.data.SpecialtyDatabaseHelper;

import java.util.List;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass. Use the
 * {@link FineCoursesListFragment#newInstance} factory method to create an
 * instance of this fragment.
 * 
 */
public class FineCoursesListFragment extends ListFragment {
	// TODO: Rename parameter arguments, choose names that match
	// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
	public static final String EXTRA_FINE_COURSE_CATEGORY = "fine_course_category";
	public static final String EXTRA_FINE_COURSE_SCHOOL_NAME = "fine_course_school_name";

	private String mCategory;
	private String mSchoolName;

	/**
	 * Use this factory method to create a new instance of this fragment using
	 * the provided parameters.
	 * 
	 * @param param1
	 *            Parameter 1.
	 * @param param2
	 *            Parameter 2.
	 * @return A new instance of fragment FineCoursesListFragment.
	 */
	// TODO: Rename and change types and number of parameters
	public static FineCoursesListFragment newInstance(String key,String category) {
		FineCoursesListFragment fragment = new FineCoursesListFragment();
		Bundle args = new Bundle();
		args.putString(key, category);
		fragment.setArguments(args);
		return fragment;
	}

	public FineCoursesListFragment() {
		// Required empty public constructor
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args = getArguments();
		if (args != null) {
			if(args.containsKey(EXTRA_FINE_COURSE_CATEGORY)) {
				mCategory = args.getString(EXTRA_FINE_COURSE_CATEGORY);
			}
			if(args.containsKey(EXTRA_FINE_COURSE_SCHOOL_NAME)) {
				mSchoolName = args.getString(EXTRA_FINE_COURSE_SCHOOL_NAME);
			}
			if(!TextUtils.isEmpty(mCategory)) {
				List<FineCourse> courses = SpecialtyDatabaseHelper.getInstance(getActivity()).findFindCoursesByCategory(mCategory);
				setListAdapter(new FineCourseAdapter(getActivity(), courses));
			}else if(!TextUtils.isEmpty(mSchoolName)) {
				List<FineCourse> courses = SpecialtyDatabaseHelper.getInstance(getActivity()).findFindCoursesBySchoolName(mSchoolName);
				setListAdapter(new FineCourseAdapter(getActivity(), courses));

			}
		}
	}

	static class FineCourseAdapter extends BaseAdapter{
		private final LayoutInflater mInflater;
		private final List<FineCourse> mData;
		public FineCourseAdapter(Context context,List<FineCourse> data) {
			mInflater = LayoutInflater.from(context);
			mData = data;
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(convertView == null) {
				convertView = mInflater.inflate(R.layout.fine_course_list_item, parent, false);
				holder  = new ViewHolder();
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.schoolName = (TextView) convertView.findViewById(R.id.school_name);
				holder.teacher = (TextView) convertView.findViewById(R.id.teacher);

				convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}

			FineCourse item = (FineCourse) getItem(position);

			holder.name.setText(item.name);
			holder.schoolName.setText(item.schoolName);
			holder.teacher.setText(item.teacher);


			return convertView;
		}

		static class ViewHolder{
			public TextView name;
			public TextView schoolName;
			public TextView teacher;
		}



	}


}
